~ chicken-core (chicken-5) /manual/Module (chicken string)
Trap1[[tags: manual]]2[[toc:]]34== Module (chicken string)56This module contains procedures which can perform various useful7string operations.89=== conc1011<procedure>(conc X ...)</procedure>1213Returns a string with the string-represenation of all arguments14concatenated together. {{conc}} could be implemented as1516<enscript highlight=scheme>17(define (conc . args)18 (apply string-append (map ->string args)) )19</enscript>20212223=== ->string2425<procedure>(->string X)</procedure>2627Returns a string-representation of {{X}}.282930=== string-chop3132<procedure>(string-chop STRING LENGTH)</procedure>3334Returns a list of substrings taken by ''chopping'' {{STRING}} every {{LENGTH}}35characters:3637<enscript highlight=scheme>38(string-chop "one two three" 4) ==> ("one " "two " "thre" "e")39</enscript>404142=== string-chomp4344<procedure>(string-chomp STRING [SUFFIX])</procedure>4546If {{STRING}} ends with {{SUFFIX}}, then this procedure returns a copy47of its first argument with the suffix removed, otherwise returns48{{STRING}} unchanged. {{SUFFIX}} defaults to {{"\n"}}.495051=== string-compare35253<procedure>(string-compare3 STRING1 STRING2)</procedure><br>54<procedure>(string-compare3-ci STRING1 STRING2)</procedure>5556Perform a three-way comparison between the {{STRING1}} and {{STRING2}},57returning either {{-1}} if {{STRING1}} is lexicographically less58than {{STRING2}}, {{0}} if it is equal, or {{1}} if it s greater.59{{string-compare3-ci}} performs a case-insensitive comparison.606162=== string-intersperse6364<procedure>(string-intersperse LIST [STRING])</procedure>6566Returns a string that contains all strings in {{LIST}} concatenated67together. {{STRING}} is placed between each concatenated string and68defaults to {{" "}}.6970<enscript highlight=scheme>71(string-intersperse '("one" "two") "three")72</enscript>7374is equivalent to7576<enscript highlight=scheme>77(apply string-append (intersperse '("one" "two") "three"))78</enscript>798081=== string-split8283<procedure>(string-split STRING [DELIMITER-STRING [KEEPEMPTY]])</procedure>8485Split string into substrings delimited by any of the characters given86in the delimiter string. If no delimiters are specified, a string87comprising the tab, newline and space characters is assumed. If the88parameter {{KEEPEMPTY}} is given and not {{#f}}, then empty substrings89are retained:9091<enscript highlight=scheme>92(string-split "one two three") ==> ("one" "two" "three")93(string-split "foo:bar::baz:" ":" #t) ==> ("foo" "bar" "" "baz" "")94(string-split "foo:bar:baz,quux,zot" ":," ) ==> ("foo" "bar" "baz" "quux" "zot")95</enscript>969798=== string-translate99100<procedure>(string-translate STRING FROM [TO])</procedure>101102Returns a fresh copy of {{STRING}} with characters matching103{{FROM}} translated to {{TO}}. If {{TO}} is omitted, then104matching characters are removed. {{FROM}} and {{TO}} may be105a character, a string or a list. If both {{FROM}} and {{TO}}106are strings, then the character at the same position in {{TO}}107as the matching character in {{FROM}} is substituted.108109110=== string-translate*111112<procedure>(string-translate* STRING SMAP)</procedure>113114Substitutes elements of {{STRING}} according to {{SMAP}}.115{{SMAP}} should be an association-list where each element of the list116is a pair of the form {{(MATCH . REPLACEMENT)}}. Every occurrence of117the string {{MATCH}} in {{STRING}} will be replaced by the string118{{REPLACEMENT}}:119120<enscript highlight=scheme>121(string-translate*122 "<h1>this is a \"string\"</h1>"123 '(("<" . "<") (">" . ">") ("\"" . """)) )124=> "<h1>this is a "string"</h1>"125</enscript>126127128=== substring=?129130<procedure>(substring=? STRING1 STRING2 [START1 [START2 [LENGTH]]])</procedure><br>131<procedure>(substring-ci=? STRING1 STRING2 [START1 [START2 [LENGTH]]])</procedure>132133Returns {{#t}} if the strings {{STRING1}} and {{STRING2}} are equal,134or {{#f}} otherwise. The comparison starts at the positions135{{START1}} and {{START2}} (which default to 0), comparing {{LENGTH}}136characters (which defaults to the minimum of the remaining length of137both strings).138139140=== substring-index141142<procedure>(substring-index MAYBE-SUBSTRING STRING [START])</procedure><br>143<procedure>(substring-index-ci MAYBE-SUBSTRING STRING [START])</procedure>144145Searches for first index in string {{STRING}} where string146{{MAYBE-SUBSTRING}} occurs. If the optional argument {{START}} is given,147then the search starts at that index. {{substring-index-ci}}148is a case-insensitive version of {{substring-index}}.149150151=== reverse-list->string152153<procedure>(reverse-list->string LIST)</procedure>154155Returns a string with the characters in {{LIST}} in reverse156order. This is equivalent to {{(list->string (reverse LIST))}}, but157much more efficient.158159160=== reverse-string-append161162<procedure>(reverse-string-append LIST)</procedure>163164{{(apply string-append (reverse LIST))}}165166167---168Previous: [[Module (chicken sort)]]169170Next: [[Module (chicken syntax)]]